package cn.net.xyan.easy.spring.core.utils;

import cn.net.xyan.easy.spring.core.exception.StatusAndMessageError;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xiashenpin on 16/1/17.
 */
public class DateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
            String v = p.readValueAs(String.class);
            return format.parse(v);
        } catch (ParseException e) {
            throw new StatusAndMessageError(-99,e);
        }
    }
}
